﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Entity;
using System.Collections.Generic;
using ClassLibrary.DAO;

public partial class usercontrol_ucOrderDetail : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if ((Session[Global.SESSION_SHIPTO] == null) || (Session[Global.SESSION_TOTAL] == null) || (Session[Global.SESSION_CART] == null))
            {
                Response.Redirect("~/ShoppingCart.aspx");
            }
            else
            {
                List<TblOrderDetail> arrItems = new List<TblOrderDetail>();
                if (Session[Global.SESSION_CART] != null)
                    arrItems = (List<TblOrderDetail>)Session[Global.SESSION_CART];
                grdShoppingCart.DataSource = arrItems;
                grdShoppingCart.DataBind();
                TblShippTo shipto;
                if (Session[Global.SESSION_SHIPTO] == null)
                    shipto = new TblShippTo();
                else
                    shipto = (TblShippTo)Session[Global.SESSION_SHIPTO];
                Session[Global.SESSION_SHIPTO] = shipto;

                lb_name.Text = shipto.Name;
                lb_phone.Text = shipto.Phone;
                lb_address.Text = shipto.Address;
                lb_email.Text = ((TblUserAccount)Session[Global.SESSION_LOGGED_USER]).Email;
                lb_OrderDate.Text = DateTime.Now.ToString("dd/MM/yyyy");

                lblTotal.Text = (float.Parse(Session[Global.SESSION_TOTAL].ToString())).ToString("0,00.##");
            }

        }
    }
    protected override void OnError(EventArgs e)
    {
        base.OnError(e);
        Exception err = Server.GetLastError();
        if (err != null)
        {
            Session[Global.SESSION_ERROR] = err;
            Response.Redirect(Global.ERROR_PAGE, true);
        }
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("~/ShoppingCart.aspx"), true);
    }


    protected void btnNext_Click(object sender, ImageClickEventArgs e)
    {
         Response.Redirect("~/PaymentANZ.aspx", true);
       // ScriptManager.RegisterStartupScript(btnNext, btnNext.GetType(), "ScriptLoginSuccess", "UpdatePayment();", true);
    }

    protected void grdShoppingCart_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
        }
    }
    protected void btnBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("~/ShippingTo.aspx", true);
    }
    protected void btnThanhToan_Click(object sender, ImageClickEventArgs e)
    {
         if ((Session[Global.SESSION_TOTAL] == null) || (Session[Global.SESSION_SHIPTO] == null) || (Session[Global.SESSION_CART] == null))
        {
            Response.Redirect("~/Default.aspx");
        }
        else
        {
            bool flag = false;
            try
            {
                //EmailService emailSrv = new EmailService();
                bool status;
                //cap nhat thong tin giao hang     
                TblShippTo shipto = (TblShippTo)Session[Global.SESSION_SHIPTO];
                TblShippToDAO shiptoDAO = new TblShippToDAO(Global.CONNECTION_STRING);
                status = shiptoDAO.Update(shipto);


                TblOrderDAO orderdao = new TblOrderDAO(Global.CONNECTION_STRING);
                TblOrder order = new TblOrder();
                TblUserAccount userAccount = (TblUserAccount)Session[Global.SESSION_LOGGED_USER];
                order.UserAccount = userAccount;
                order.AccountID = userAccount.AccountID;
                
                //  order.ShippingMethodID = 3;

                float dDeliveryFee = 0;
                //dDeliveryFee = float.Parse(((TblShippTo)Session[Global.SESSION_SHIPTO]).DeliveryFee.ToString());

                order.DeliveryFee = dDeliveryFee;


                // order.Total = float.Parse(Session[GlobalWeb.SESSION_TOTAL].ToString()) + float.Parse(dDeliveryFee.ToString());
                order.Total = float.Parse(Session[Global.SESSION_TOTAL].ToString());
                // order.Total = float.Parse(Session["SumTotal"].ToString());

                List<TblOrderDetail> arrItems = (List<TblOrderDetail>)Session[Global.SESSION_CART];
                int id;
                orderdao.Insert(order, arrItems, out id);

                order.OrderID = id;
                order.OrderDate = Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy"));
                //order.OrderDate = Convert.ToDateTime(DateTime.Now.ToString("MM/dd/yyyy"));

                // Total (tong tien san pham + diliveryfee)  khi send mail
                order.Total = float.Parse(Session[Global.SESSION_TOTAL].ToString()) + float.Parse(dDeliveryFee.ToString());
                //send mail
                //emailSrv.SendOrderEmail(userAccount, Server.MapPath(Global.ORDER_EMAIL_DEPOSIT), order, arrItems);

                //send mail to sale department
                //emailSrv.SendOrderEmailToSales(userAccount, Server.MapPath(Global.ORDER_EMAIL_SALES), order, arrItems);

                Session[Global.SESSION_CART] = null;
                Session[Global.SESSION_SHIPTO] = null;
                flag = true;

            }
            catch (Exception ex)
            {
                Session[Global.SESSION_ERROR] = ex;
                Response.Redirect(Global.ERROR_PAGE, true);
            }
            if (flag)
            {
               // ScriptManager.RegisterStartupScript(btnThanhToan, btnThanhToan.GetType(), "ScriptLoginSuccess", "UpdatePayment();", true);
                ScriptManager.RegisterStartupScript(btnThanhToan, btnThanhToan.GetType(), "alert", "alert(' Bạn đã đặt hàng thành công. Xin vui lòng kiểm tra Email!');", true);
                ScriptManager.RegisterStartupScript(btnThanhToan, btnThanhToan.GetType(), "ScriptLoginSuccess", "UpdatePayment();", true);
            }
        }
    }
}
